/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Bosta.java
 *
 * Created on 18/Dez/2011, 10:51:33
 */

package intopo.game.impl.givetake;

import java.awt.BorderLayout;
import java.awt.CardLayout;

/**
 *
 * @author <a href="mailto:mariano.pedro@gmail.com">Pedro Mariano</a>
 *
 * @version 1.0 18/Dez/2011
 */
public class GiveTakeGameComponent extends intopo.game.gui.AbstractGameEditorComponent<GiveTakeGame, GiveTakeStrategy>
{

	/** Creates new form BeanForm */
	public GiveTakeGameComponent ()
	{
		this.game = new GiveTakeGame (0.5f, 1f, 2f);
		initComponents();
		this.add (this.infoPanel, BorderLayout.SOUTH);
		this.updateInfoPanel (true);
	}

	@Override
	public String getConfigFileDescription () {
		throw new UnsupportedOperationException ("Not supported yet.");
	}

	public GiveTakeGame getGame ()
	{
		String type = (String) this.typeComboBox.getSelectedItem ();
		float bg = (Float) this.bonusGiveFormattedTextField.getValue ();
		float cpt = (Float) this.costPerformerTakeFormattedTextField.getValue ();
		float cst = (Float) this.costSubjectTakeFormattedTextField.getValue ();
		if (type.equals ("simulated")) {
			float oneMoreRoundProbability = (Float) this.oneMoreStageProbabilityFormattedTextField.getValue ();
			int minimumNumberOfIterations = (Integer) this.minimumNumberStagesFormattedTextField.getValue ();
			return new GiveTakeSimulationGame (bg, cpt, cst, oneMoreRoundProbability, minimumNumberOfIterations);
		}
		else if (type.equals ("infinite")) {
			return new GiveTakeGame (bg, cpt, cst);
		}
		else {
			throw new Error ("Unreachable code in GiveTakeGameComponent.getGame ()");
		}
	}
	public void setGame (GiveTakeGame game)
	{
		this.game = game;
		this.bonusGiveFormattedTextField.setValue (new Float (game.giveBonus));
		this.costPerformerTakeFormattedTextField.setValue (new Float (game.takePerformerCost));
		this.costSubjectTakeFormattedTextField.setValue (new Float (game.takeSubjectCost));
		if (game instanceof GiveTakeSimulationGame) {
			GiveTakeSimulationGame g1 = (GiveTakeSimulationGame) game;
			this.typeComboBox.setSelectedItem ("simulated");
			this.oneMoreStageProbabilityFormattedTextField.setValue (new Float (g1.getOneMoreRoundProbability ()));
			this.minimumNumberStagesFormattedTextField.setValue (new Integer (g1.getMinimumNumberOfIterations ()));
		}
		else if (game instanceof GiveTakeGame) {
			this.typeComboBox.setSelectedItem ("infinite");
		}
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {
      java.awt.GridBagConstraints gridBagConstraints;

      parametersPanel = new javax.swing.JPanel();
      standardParametersPanel = new javax.swing.JPanel();
      javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
      bonusGiveFormattedTextField = new javax.swing.JFormattedTextField();
      javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
      costPerformerTakeFormattedTextField = new javax.swing.JFormattedTextField();
      javax.swing.JLabel jLabel4 = new javax.swing.JLabel();
      costSubjectTakeFormattedTextField = new javax.swing.JFormattedTextField();
      variantSelectorPanel = new javax.swing.JPanel();
      jLabel1 = new javax.swing.JLabel();
      typeComboBox = new javax.swing.JComboBox();
      gameTypeParametersPanel = new javax.swing.JPanel();
      deterministicPanel = new javax.swing.JPanel();
      simulatedPanel = new javax.swing.JPanel();
      javax.swing.JLabel jLabel5 = new javax.swing.JLabel();
      oneMoreStageProbabilityFormattedTextField = new javax.swing.JFormattedTextField();
      javax.swing.JLabel jLabel6 = new javax.swing.JLabel();
      minimumNumberStagesFormattedTextField = new javax.swing.JFormattedTextField();

      setBorder(javax.swing.BorderFactory.createTitledBorder("game parameters"));
      setLayout(new java.awt.BorderLayout());

      parametersPanel.setLayout(new javax.swing.BoxLayout(parametersPanel, javax.swing.BoxLayout.PAGE_AXIS));

      standardParametersPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("standard parameters"));
      standardParametersPanel.setLayout(new java.awt.GridBagLayout());

      jLabel2.setText("bg: ");
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
      standardParametersPanel.add(jLabel2, gridBagConstraints);

      bonusGiveFormattedTextField.setColumns(5);
      bonusGiveFormattedTextField.setValue(new Float (0.5));
      bonusGiveFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            bonusGiveFormattedTextFieldPropertyChange(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      standardParametersPanel.add(bonusGiveFormattedTextField, gridBagConstraints);

      jLabel3.setText("cpt: ");
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
      standardParametersPanel.add(jLabel3, gridBagConstraints);

      costPerformerTakeFormattedTextField.setColumns(5);
      costPerformerTakeFormattedTextField.setValue(new Float (1));
      costPerformerTakeFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            costPerformerTakeFormattedTextFieldPropertyChange(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      standardParametersPanel.add(costPerformerTakeFormattedTextField, gridBagConstraints);

      jLabel4.setText("cst: ");
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
      standardParametersPanel.add(jLabel4, gridBagConstraints);

      costSubjectTakeFormattedTextField.setColumns(5);
      costSubjectTakeFormattedTextField.setValue(new Float (2));
      costSubjectTakeFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            costSubjectTakeFormattedTextFieldPropertyChange(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      standardParametersPanel.add(costSubjectTakeFormattedTextField, gridBagConstraints);

      parametersPanel.add(standardParametersPanel);

      jLabel1.setText("variant: ");
      variantSelectorPanel.add(jLabel1);

      typeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "infinite", "simulated" }));
      typeComboBox.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            typeComboBoxActionPerformed(evt);
         }
      });
      variantSelectorPanel.add(typeComboBox);

      parametersPanel.add(variantSelectorPanel);

      gameTypeParametersPanel.setLayout(new java.awt.CardLayout());
      gameTypeParametersPanel.add(deterministicPanel, "infinite");

      simulatedPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("simulated variant parameters"));
      simulatedPanel.setLayout(new java.awt.GridBagLayout());

      jLabel5.setText("one more stage probability: ");
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      simulatedPanel.add(jLabel5, gridBagConstraints);

      oneMoreStageProbabilityFormattedTextField.setColumns(5);
      oneMoreStageProbabilityFormattedTextField.setValue(new Float (0.1f));
      oneMoreStageProbabilityFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            oneMoreStageProbabilityFormattedTextFieldPropertyChange(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      simulatedPanel.add(oneMoreStageProbabilityFormattedTextField, gridBagConstraints);

      jLabel6.setText("minimum number stages: ");
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      simulatedPanel.add(jLabel6, gridBagConstraints);

      minimumNumberStagesFormattedTextField.setColumns(5);
      minimumNumberStagesFormattedTextField.setValue(new Integer (100));
      minimumNumberStagesFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            minimumNumberStagesFormattedTextFieldPropertyChange(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      simulatedPanel.add(minimumNumberStagesFormattedTextField, gridBagConstraints);

      gameTypeParametersPanel.add(simulatedPanel, "simulated");

      parametersPanel.add(gameTypeParametersPanel);

      add(parametersPanel, java.awt.BorderLayout.CENTER);
   }// </editor-fold>//GEN-END:initComponents

	 private void typeComboBoxActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_typeComboBoxActionPerformed
		 CardLayout layout = (CardLayout) gameTypeParametersPanel.getLayout ();
		 String type = (String) this.typeComboBox.getSelectedItem ();
		 layout.show (this.gameTypeParametersPanel, type);
}//GEN-LAST:event_typeComboBoxActionPerformed

	 private void bonusGiveFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_bonusGiveFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			final float value = (Float) this.bonusGiveFormattedTextField.getValue ();
			final boolean ok = value >= 0 && value < 1;
			this.update (this.bonusGiveFormattedTextField, ok);
			if (ok) {
				this.updateInfoPanel (true);
			}
		}
	 }//GEN-LAST:event_bonusGiveFormattedTextFieldPropertyChange

	 private void costPerformerTakeFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_costPerformerTakeFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			final float value = (Float) this.costPerformerTakeFormattedTextField.getValue ();
			final boolean ok = value >= 0;
			this.update (this.costPerformerTakeFormattedTextField, ok);
			if (ok) {
				this.updateInfoPanel (true);
			}
		}
	 }//GEN-LAST:event_costPerformerTakeFormattedTextFieldPropertyChange

	 private void costSubjectTakeFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_costSubjectTakeFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			final float value = (Float) this.costSubjectTakeFormattedTextField.getValue ();
			final boolean ok = value >= 0;
			this.update (this.costSubjectTakeFormattedTextField, ok);
			if (ok) {
				this.updateInfoPanel (true);
			}
		}
	 }//GEN-LAST:event_costSubjectTakeFormattedTextFieldPropertyChange

	 private void oneMoreStageProbabilityFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_oneMoreStageProbabilityFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			final float value = (Float) this.oneMoreStageProbabilityFormattedTextField.getValue ();
			final boolean ok = value >= 0 && value <= 1;
			this.update (this.oneMoreStageProbabilityFormattedTextField, ok);
		}
	 }//GEN-LAST:event_oneMoreStageProbabilityFormattedTextFieldPropertyChange

	 private void minimumNumberStagesFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_minimumNumberStagesFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			final int value = (Integer) this.minimumNumberStagesFormattedTextField.getValue ();
			final boolean ok = value >= 0;
			this.update (this.minimumNumberStagesFormattedTextField, ok);
		}
	 }//GEN-LAST:event_minimumNumberStagesFormattedTextFieldPropertyChange


   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JFormattedTextField bonusGiveFormattedTextField;
   private javax.swing.JFormattedTextField costPerformerTakeFormattedTextField;
   private javax.swing.JFormattedTextField costSubjectTakeFormattedTextField;
   private javax.swing.JPanel deterministicPanel;
   private javax.swing.JPanel gameTypeParametersPanel;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JFormattedTextField minimumNumberStagesFormattedTextField;
   private javax.swing.JFormattedTextField oneMoreStageProbabilityFormattedTextField;
   private javax.swing.JPanel parametersPanel;
   private javax.swing.JPanel simulatedPanel;
   private javax.swing.JPanel standardParametersPanel;
   private javax.swing.JComboBox typeComboBox;
   private javax.swing.JPanel variantSelectorPanel;
   // End of variables declaration//GEN-END:variables

}

// Local Variables:
// mode: java
// mode: flyspell-prog
// ispell-local-dictionary: "british"
// End:
